//
//  UIView+Frame.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)

/**设置x坐标*/
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
/**获取x坐标*/
- (CGFloat)x
{
    return self.frame.origin.x;
}
/**设置y坐标*/
- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
/**获取y坐标*/
- (CGFloat)y
{
    return self.frame.origin.y;
}
/**设置width*/
- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
/**获取width*/
- (CGFloat)width
{
    return self.frame.size.width;
}
/**设置height*/
- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
/**获取height*/
- (CGFloat)height
{
    return self.frame.size.height;
}
/**设置size*/
- (void)setSize:(CGSize)size
{
    [self setWidth:size.width];
    [self setHeight:size.height];
}
/**获取size*/
- (CGSize)size
{
    return self.frame.size;
}
/**设置origin*/
- (void)setOrigin:(CGPoint)origin
{
    [self setX:origin.x];
    [self setY:origin.y];
}
/**获取origin*/
- (CGPoint)origin
{
    return self.frame.origin;
}
/**获取最大的x*/
- (CGFloat)maxX
{
    return CGRectGetMaxX(self.frame);
}
/**获取最大的y*/
- (CGFloat)maxY
{
    return CGRectGetMaxY(self.frame);
}
/**获取最小的x*/
- (CGFloat)minX {
    
    return CGRectGetMinX(self.frame);
}
/** 获取最小的y*/
- (CGFloat)minY {
    
    return CGRectGetMinY(self.frame);
}
/**  获取中间x值*/
- (CGFloat)centerX {
    
    return CGRectGetMidX(self.frame);
}
/** 获取中间y值*/
- (CGFloat)centerY {
    
    return CGRectGetMidY(self.frame);
}
- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}
- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}
- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void) setTop: (CGFloat) newtop
{
    CGRect newframe = self.frame;
    newframe.origin.y = newtop;
    self.frame = newframe;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void) setLeft: (CGFloat) newleft
{
    CGRect newframe = self.frame;
    newframe.origin.x = newleft;
    self.frame = newframe;
}

- (CGFloat) bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void) setBottom: (CGFloat) newbottom
{
    CGRect newframe = self.frame;
    newframe.origin.y = newbottom - self.frame.size.height;
    self.frame = newframe;
}

- (CGFloat) right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void) setRight: (CGFloat) newright
{
    CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
    CGRect newframe = self.frame;
    newframe.origin.x += delta ;
    self.frame = newframe;
}


- (void)stayIntrinsicSize {
    [self stayIntrinsicWidth];
    [self stayIntrinsicHeight];
}

- (void)stayIntrinsicWidth {
    [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}

- (void)stayIntrinsicHeight {
    [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}

- (CGSize)compressedSize {
    return [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultHigh];
}

@end
